local localPlayer = getLocalPlayer()
local playerName = getPlayerName(localPlayer)

function createPasswordWindow()
	windowChangepw = guiCreateWindow(0.3859,0.349,0.2219,0.1628,"[MTA:RPG] Change password",true)
	guiSetSize(windowChangepw, 165, 21)
	guiSetAlpha(windowChangepw,0.8)
	editOldpw = guiCreateEdit(110,29,165,21,"",false,windowChangepw)
	guiSetAlpha(editOldpw,1)
	guiEditSetMasked(editOldpw,true)
	labelOldpw = guiCreateLabel(10,29,90,21,"Old password:",false,windowChangepw)
	guiSetAlpha(labelOldpw,1)
	guiLabelSetColor(labelOldpw,255,255,255)
	guiLabelSetVerticalAlign(labelOldpw,"center")
	guiLabelSetHorizontalAlign(labelOldpw,"left",false)
	editNewpw = guiCreateEdit(110,60,165,21,"",false,windowChangepw)
	guiSetAlpha(editNewpw,1)
	guiEditSetMasked(editNewpw,true)
	guiEditSetMaxLength(editNewpw,50)
	labelNewpw = guiCreateLabel(10,60,90,21,"New password:",false,windowChangepw)
	guiSetAlpha(labelNewpw,1)
	guiLabelSetColor(labelNewpw,255,255,255)
	guiLabelSetVerticalAlign(labelNewpw,"center")
	guiLabelSetHorizontalAlign(labelNewpw,"left",false)
	buttonChangepw = guiCreateButton(10,91,265,23,"Change password",false,windowChangepw)
	guiSetAlpha(buttonChangepw,1)
	
	guiSetVisible(windowChangepw, false)
	
	addEventHandler("onClientGUIClick", buttonChangepw, clientSubmitChangepw, false)
end

function createLoginWindow()

	backgroundLogo = guiCreateStaticImage(0,0,1,1,"logo.png",true)

	windowLogin = guiCreateWindow(0.3945,0.3646,0.2109,0.2018,"[MTA:RPG] Login",true)
	guiSetSize(windowLogin, 270, 155, false)
	guiSetAlpha(windowLogin,0.8)
	labelUsername = guiCreateLabel(10,52,59,24,"Username:",false,windowLogin)
	guiSetAlpha(labelUsername,1)
	guiLabelSetColor(labelUsername,255,255,255)
	guiLabelSetVerticalAlign(labelUsername,"center")
	guiLabelSetHorizontalAlign(labelUsername,"left",false)
	labelPassword = guiCreateLabel(10,86,59,24,"Password:",false,windowLogin)
	guiSetAlpha(labelPassword,1)
	guiLabelSetColor(labelPassword,255,255,255)
	guiLabelSetVerticalAlign(labelPassword,"center")
	guiLabelSetHorizontalAlign(labelPassword,"left",false)
	labelInfo = guiCreateLabel(10,26,250,17,"Please login or register.",false,windowLogin)
	guiSetAlpha(labelInfo,1)
	guiLabelSetColor(labelInfo,255,255,255)
	guiLabelSetVerticalAlign(labelInfo,"top")
	guiLabelSetHorizontalAlign(labelInfo,"center",false)
	guiSetFont(labelInfo,"default-bold-small")
	editUsername = guiCreateEdit(79,52,181,25,playerName,false,windowLogin)
	guiSetAlpha(editUsername,1)
	guiEditSetMaxLength(editUsername, 50)
	editPassword = guiCreateEdit(79,86,181,25,"",false,windowLogin)
	guiSetAlpha(editPassword,1)
	guiEditSetMasked(editPassword, true)
	guiEditSetMaxLength(editPassword, 50)
	buttonLogin = guiCreateButton(10,121,120,21,"Log in",false,windowLogin)
	guiSetAlpha(buttonLogin,1)
	buttonRegister = guiCreateButton(143,121,117,21,"Register",false,windowLogin)
	guiSetAlpha(buttonRegister,1)
	
	guiSetVisible(backgroundLogo, false)
	guiSetVisible(windowLogin, false)
	
	addEventHandler("onClientGUIClick", buttonLogin, clientSubmitLogin, false)
	addEventHandler("onClientGUIClick", buttonRegister, clientSubmitRegister, false)
end

function resourceStart()
	createLoginWindow()
	if (windowLogin ~= nil) then
		guiSetVisible(windowLogin, true)
		-- guiSetVisible(backgroundLogo, true)
	else
		outputChatBox("An error has occurred.")
	end
	showCursor(true)
	guiSetInputEnabled(true)
end

function changePw()
	createPasswordWindow()
	guiSetVisible(windowChangepw, true)
	showCursor(true)
	guiSetInputEnabled(true)
end

function clientSubmitLogin(button, state)
	if button == "left" and state == "up" then
		local username = guiGetText(editUsername)
		local password = guiGetText(editPassword)
		if username and password then
			triggerServerEvent("submitLogin", getRootElement(), localPlayer, username, password)
		else
			guiSetText(labelInfo, "Enter username and password.")
		end
	end
end

function clientSubmitRegister(button, state)
	if button == "left" and state == "up" then
		local username = guiGetText(editUsername)
		local password = guiGetText(editPassword)
		if username and password then
			triggerServerEvent("submitRegister", getRootElement(), localPlayer, username, password)
		else
			guiSetText(labelInfo, "Enter username and password.")
		end
	end
end

function clientSubmitChangepw(button, state)
	if button == "left" and state == "up" then
		local oldpassword = guiGetText(editOldpw)
		local newpassword = guiGetText(editNewpw)
		if oldpassword and newpassword then
			triggerServerEvent("submitChangepw", getRootElement(), localPlayer, oldpassword, newpassword)
		else
			outputChatBox("Enter old and new password.")
		end
	end
end

function hideLoginWindow()
	guiSetInputEnabled(false)
	guiSetVisible(windowLogin, false)
	guiSetVisible(backgroundLogo, false)
	showCursor(false)
end

function hidePasswordWindow()
	guiSetInputEnabled(false)
	guiSetVisible(windowChangepw, false)
	showCursor(false)
end

function unknownError()
	guiSetText(labelInfo, "An unknown error occured.")
end

function loginWrong()
	guiSetText(labelInfo, "Wrong username and/or password.")
end

function registerTaken()
	guiSetText(labelInfo, "This username is allready taken.")
end

addEvent("hidePasswordWindow", true)
addEvent("hideLoginWindow", true)
addEvent("unknownError", true)
addEvent("loginWrong", true)
addEvent("registerTaken", true)
addEventHandler("hidePasswordWindow", getRootElement(), hidePasswordWindow)
addEventHandler("hideLoginWindow", getRootElement(), hideLoginWindow)
addEventHandler("unknownError", getRootElement(), unknownError)
addEventHandler("loginWrong", getRootElement(), loginWrong)
addEventHandler("registerTaken", getRootElement(), registerTaken)
addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), resourceStart)

addCommandHandler("changepw", changePw)